/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sie_invdis;

/**
 *
 * @author Michelle
 */


import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;

import org.jfree.ui.ApplicationFrame;

import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import javax.swing.Timer;
import java.text.*;

public class ventas_sie_cortes_graficacion extends ApplicationFrame{

 
    private JFreeChart chart=null;
    private ChartPanel panel=null;
 
 



 Timer timer = new Timer (300000, new ActionListener ()
{
    public void actionPerformed(ActionEvent e)
    {
        // Aquí el código que queramos ejecutar.
    if (isVisible()==true)
            {
                     loaddatos(1);
            }
    
    }
});

  public ventas_sie_cortes_graficacion(String title,int opcion)
    {
        super(title);
        this.loaddatos(opcion);
       
     
    }

    public void loaddatos(int popcion){

       //CategoryDataset dataset= crearDataset(pFecha,pdias);//no se crea el data set se carga de variables_global
        java.util.Date fechaactualiza=new Date();
        this.setTitle("Hora de Ultima Actualizacion:"+fechaactualiza.getHours()+":"+fechaactualiza.getMinutes());
        DecimalFormat miforma = new DecimalFormat();
        if(popcion==0)
            {
            chart=crearChart(variables_global.getdatasetcortedia(),variables_global.gettitulografica_gral()+": Total="+miforma.format(variables_global.getcortetotal()),variables_global.gettitulografica_ejeori());
            variables_global.setmuestrograficacorte(0);
            timer.start();
            }
        if(popcion==1)
            {
           
            chart=crearChart(variables_global.getdatasetcortedia(),variables_global.gettitulografica_gral()+": Total="+miforma.format(variables_global.getcortetotal()),variables_global.gettitulografica_ejeori());
            timer.start();
            }
        
        if(popcion==2)
            {
            chart=crearChart(variables_global.getdatasettotales(),variables_global.gettitulografica_gral()+": Total="+miforma.format(variables_global.getcortetotal()),variables_global.gettitulografica_ejeori());
            }
        if(popcion==3)
            {
            chart=crearChart(variables_global.getdatasettotalesdia(),variables_global.gettitulografica_gral()+": Total="+miforma.format(variables_global.getcortetotal()),variables_global.gettitulografica_ejeori());
            }
        if(popcion==4)
            {
            chart=crearChart(variables_global.getdatasetagentes(),variables_global.gettitulografica_agentes()+": Total="+miforma.format(variables_global.gettotalagentes()),variables_global.gettitulografica_ejeori());
            }
        if(popcion==5)
            {
            chart=crearChart(variables_global.getdatasetarmasvp(),variables_global.gettitulografica_artmasvxp(),"Proveedor");
            }
         if(popcion==6)
            {
            chart=crearChart(variables_global.getdatasetarmasvf(),variables_global.gettitulografica_armasvf(),"Familias");
            }
        if(popcion==7)
            {
            chart=crearChart(variables_global.getdatasetresxprov(),variables_global.gettitulografica_resxprov(),"Subfamilia");
            }

        panel = new ChartPanel(chart,false);

        panel.setPreferredSize(new Dimension(800,600));
        panel.updateUI();
        this.setContentPane(panel);
        this.setVisible(false);
        //this.update(this.getGraphics());

        this.setVisible(true);




     }
    
    private static JFreeChart crearChart(CategoryDataset data,String subtitulo,String tituejeori)
    {
        //createLineChart
        JFreeChart chart= ChartFactory.createBarChart3D(
                subtitulo,        //Nombre de la gráfica
                tituejeori,               //Nombre del eje Horizontal
                "Monto de Venta",                  //Nombre del eje vertical
                data,                       //Data
                PlotOrientation.VERTICAL, //Orientación
                true,                       //Incluir leyenda
                true,                       //Información al pasar el mouse
                true);                      //URls
        //Dar color al fondo del panel
        chart.setBackgroundPaint(Color.ORANGE);
        //Dar color a cada categoria
        CategoryPlot plot =(CategoryPlot) chart.getPlot();
        plot.setBackgroundPaint(Color.BLUE);
        //Color del fondo del gráfico
        plot.setBackgroundPaint(Color.WHITE);
        //Lineas divisorias
        plot.setDomainGridlinesVisible(true);
        plot.setRangeGridlinePaint(Color.BLACK);
        //Calculo de los valores en el eje x
        final NumberAxis rangeAxis=(NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        BarRenderer renderer = (BarRenderer) plot.getRenderer();
        renderer.setDrawBarOutline(false);
        //Dar color a cada barra
        GradientPaint gp0= new GradientPaint(0.0f,0.0f,Color.BLUE,0.0f,0.0f,new Color(0,0,64));
        GradientPaint gp1= new GradientPaint(0.0f,0.0f,Color.green,0.0f,0.0f,new Color(0,64,0));
        GradientPaint gp2= new GradientPaint(0.0f,0.0f,Color.red,0.0f,0.0f,new Color(64,0,0));
        renderer.setSeriesPaint(0,gp0);
        renderer.setSeriesPaint(1,gp1);
        renderer.setSeriesPaint(2,gp2);

        CategoryAxis domainAxis = plot.getDomainAxis();
        domainAxis.setCategoryLabelPositions(
                CategoryLabelPositions.createUpRotationLabelPositions(Math.PI/6.0));
        return chart;
    }

 
 
     @Override public void windowClosing(WindowEvent e) {

      variables_global.setmuestrograficacorte(1);
      System.out.println("Provando la Actualizacion="+variables_global.getmuestrograficacorte());
      this.timer.stop();
      this.dispose();


      
     }

  
}
